package com.runen.wnhz.runen.ui.fragment.major;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import com.runen.wnhz.runen.ui.activity.main.SearchActivity;

import com.bumptech.glide.Glide;
import com.runen.wnhz.runen.R;
import com.runen.wnhz.runen.common.utils.JumpUtlis;
import com.runen.wnhz.runen.common.utils.ToastUtil;
import com.runen.wnhz.runen.data.entity.LessonListEntity;
import com.runen.wnhz.runen.data.entity.LessonTypeBean;
import com.runen.wnhz.runen.di.component.ApplicationComponent;
import com.runen.wnhz.runen.di.component.DaggerMajorComponent;
import com.runen.wnhz.runen.di.module.MajorModule;
import com.runen.wnhz.runen.presenter.Contart.MajorContart;
import com.runen.wnhz.runen.presenter.iPresenter.IMajorPersenter;
import com.runen.wnhz.runen.ui.activity.main.SearchResultActivity;
import com.runen.wnhz.runen.ui.activity.major.CourseDetailsActivity;
import com.runen.wnhz.runen.ui.activity.major.CurrDetailsActivity;
import com.runen.wnhz.runen.ui.adapter.puadapter.LessonHomeAdapter;
import com.runen.wnhz.runen.ui.fragment.BaseFragment;
import com.runen.wnhz.runen.widget.TitleBuilder;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * Created by Administrator on 2018-04-08.
 */

public class MajorFragment extends BaseFragment<IMajorPersenter> implements View.OnClickListener, MajorContart.View {

    public static final String TAG = "MajorFragment";
    //专业
    @BindView(R.id.rl_professional)
    RelativeLayout rl_professional;
    //公共
    @BindView(R.id.rl_publics)
    RelativeLayout rl_publics;
    //项目
    @BindView(R.id.rl_project)
    RelativeLayout rl_project;

    @BindView(R.id.iv_Professional)
    ImageView ivProfessional;
    @BindView(R.id.tv_professional)
    TextView tvProfessional;
    @BindView(R.id.iv_publics)
    ImageView ivPublics;
    @BindView(R.id.tv_publics)
    TextView tvPublics;
    @BindView(R.id.iv_project)
    ImageView ivProject;
    @BindView(R.id.tv_project)
    TextView tvProject;
    @BindView(R.id.ry_professional)
    RecyclerView ryProfessional;
    @BindView(R.id.ry_publics)
    RecyclerView ryPublics;
    @BindView(R.id.ry_project)
    RecyclerView ryProject;
    @BindView(R.id.edit_jiangTangSearch)
    EditText editJiangTangSearch;
    Unbinder unbinder;

    //用于保存各个类型的课程列表
    private LessonListEntity mProfessional, mPublics, mProject;


    @Override
    public void setupAcitivtyComponent(ApplicationComponent appComponent) {
        DaggerMajorComponent.builder().applicationComponent(appComponent)
                .majorModule(new MajorModule(this))
                .build().initject(this);
    }

    @Override
    protected int getLayoutView() {
        return R.layout.major_fragment_layout;
    }

    @Override
    public void initUiAndListener(View view) {
        initView(view);
    }


    @Override
    protected void init() {
        super.init();
        //设置点击事件
        initOnClick();

        mPresenter.getLessonType(new HashMap<String, String>());
        editJiangTangSearch.setFocusable(false);
        editJiangTangSearch.setFocusableInTouchMode(false);
    }

    private void initOnClick() {
        rl_professional.setOnClickListener(this);
        rl_publics.setOnClickListener(this);
        rl_project.setOnClickListener(this);
        editJiangTangSearch.setOnClickListener(this);
    }


    @Override
    public void initToolBar(TitleBuilder mTitleBuilder) {
        setToolBarVisible(View.GONE);
    }

    private void initView(View view) {
    }

    @Override
    public void showLoadding() {

    }

    @Override
    public void dimissLoding() {

    }

    @Override
    public void showErrorMessage(String msg) {

    }


    @Override
    public void onTitleButtonClicked(TitleBuilder.TitleButton clicked) {

    }


    /* 项目管理 和 公众课程跳转到一样adapter  而专业课程直接跳转到带着播放器的界面*/
    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.rl_professional:
                goToCurrDetailsActivity(mProfessional);
                break;
            case R.id.rl_publics:
                goToCurrDetailsActivity(mPublics);
                break;
            case R.id.rl_project:
                goToCurrDetailsActivity(mProject);
                break;
            case R.id.edit_jiangTangSearch:
                startActivity(new Intent(getActivity(), SearchActivity.class));
                break;
        }
    }

    private void goToCurrDetailsActivity(LessonListEntity lessonListEntity) {
        if (lessonListEntity != null) {
            Bundle bundle = new Bundle();
            bundle.putSerializable("LessonList", lessonListEntity);
            JumpUtlis.getInstance().jumpActivity(getActivity(), CurrDetailsActivity.class, bundle);
        } else {
            ToastUtil.showToast("数据为空");
        }
    }

    @Override
    public void getLessonTypeRequest(List<LessonTypeBean> data) {
        Log.e("====getLessonType", data.get(0).toString());

        Glide.with(this).load(data.get(0).getPic()).into(ivProfessional);
        tvProfessional.setText(data.get(0).getTitle());
        Glide.with(this).load(data.get(1).getPic()).into(ivPublics);
        tvPublics.setText(data.get(1).getTitle());
        Glide.with(this).load(data.get(2).getPic()).into(ivProject);
        tvProject.setText(data.get(2).getTitle());

        for (int i = 0; i < data.size(); i++) {
            mPresenter.getLessonList(1, 0, 10, Integer.valueOf(data.get(i).getForm_id()));
        }
    }

    @Override
    public void getProfessionalRequest(LessonListEntity lessonList) {
        mProfessional = lessonList;
        if (lessonList.getList().size() > 4) {
            List<LessonListEntity.ListBean> list = new ArrayList<>();
            for (int i = 0; i < 4; i++) {
                list.add(lessonList.getList().get(i));
            }
            lessonList.setList(list);
        }

        LessonHomeAdapter adapter = new LessonHomeAdapter(getContext(), lessonList, false);
        GridLayoutManager manager = new GridLayoutManager(getActivity(), 2);
        ryProfessional.setLayoutManager(manager);
        ryProfessional.setAdapter(adapter);

        adapter.setItemClickListener(new LessonHomeAdapter.MyItemClickListener() {
            @Override
            public void onItemClick(View view, LessonListEntity.ListBean lessonEntity) {
                Log.e("----onItemClick", lessonEntity.toString());
                startCourseDetailActivity(lessonEntity);
            }
        });
    }

    @Override
    public void getPublicRequest(LessonListEntity lessonList) {
        mPublics = lessonList;
        if (lessonList.getList().size() > 4) {
            List<LessonListEntity.ListBean> list = new ArrayList<>();
            for (int i = 0; i < 4; i++) {
                list.add(lessonList.getList().get(i));
            }
            lessonList.setList(list);
        }
        LessonHomeAdapter adapter = new LessonHomeAdapter(getContext(), lessonList, false);
        GridLayoutManager manager = new GridLayoutManager(getActivity(), 2);
        ryPublics.setLayoutManager(manager);
        ryPublics.setAdapter(adapter);

        adapter.setItemClickListener(new LessonHomeAdapter.MyItemClickListener() {
            @Override
            public void onItemClick(View view, LessonListEntity.ListBean lessonEntity) {
                Log.e("----onItemClick", lessonEntity.toString());
                startCourseDetailActivity(lessonEntity);
            }
        });
    }

    private void startCourseDetailActivity(LessonListEntity.ListBean lessonEntity) {
        Intent intent = new Intent(getActivity(), CourseDetailsActivity.class);
        intent.putExtra("lid", lessonEntity.getLid());
        startActivity(intent);
    }


    @Override
    public void getProjectRequest(LessonListEntity lessonList) {
        mProject = lessonList;
        if (lessonList.getList().size() > 4) {
            List<LessonListEntity.ListBean> list = new ArrayList<>();
            for (int i = 0; i < 4; i++) {
                list.add(lessonList.getList().get(i));
            }
            lessonList.setList(list);
        }

     /*   LessonHomeAdapter adapter = new LessonHomeAdapter(getContext(), lessonList, false);
        GridLayoutManager manager = new GridLayoutManager(getActivity(), 2);
        ryProject.setLayoutManager(manager);
        ryProject.setAdapter(adapter);

        adapter.setItemClickListener(new LessonHomeAdapter.MyItemClickListener() {
            @Override
            public void onItemClick(View view, LessonListEntity.ListBean lessonEntity) {
                Log.e("----onItemClick", lessonEntity.toString());
                startCourseDetailActivity(lessonEntity);
            }
        });*/
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // TODO: inflate a fragment view
        View rootView = super.onCreateView(inflater, container, savedInstanceState);
        unbinder = ButterKnife.bind(this, rootView);
        return rootView;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }
}
